# Copyright (C) 2007 Richard Boulton
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
r"""utils.py: General utility functions.

"""

def tounicode(val):
    """Convert a value to unicode.

    If it was a unicode object, returns it unchanged.

    If it was a str object, assumes it was utf-8 encoded.

    Other objects are just converted with the default unicode() handler.

    """
    if isinstance(val, str):
        return val.decode('utf-8')
    return unicode(val)

def toutf8(val):
    """Convert a value to a UTF-8 encoded string.

    If it was a str object, assumes it was utf-8 encoded.

    If it was a unicode object, uses the standard utf-8 encoding method.

    Other objects are just converted with the default str() handler.

    """
    if isinstance(val, unicode):
        return val.encode('utf-8')
    return str(val)
